﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace ZooSystem
{
    /// <summary>
    /// Добавить сериализацию пользователей и картинку в окно (опционально), а так всё готово
    /// </summary>
    public partial class Registration : Window
    {
        Serialization database;

        public Registration()
        {
            database = Serialization.getDatabase();
            InitializeComponent();
        }

        private void OK_btn_Click(object sender, RoutedEventArgs e)
        {
            string login = login_field.Text;
            string pass1 = pass1_field.Password;
            string pass2 = pass2_field.Password;
            string email = email_field.Text;
            UserData new_user;
            bool unique = true;
            foreach (UserData user in database.UsersList)
                if (user.login == login || user.email == email)
                    unique = false;

            if (unique 
                && pass1 == pass2 
                && pass1.Length > 3 
                && login.Length > 3 
                && email.Contains('@'))
                //Checking entered data for validness:
                //Password length - more than 3 digits
                //Passwords matches
                // email contains @
            {
                new_user = new UserData(login, pass1, email);
                database.AddUser(new_user);
                this.Close();
            }
            else
            {
                MessageBoxResult result = MessageBox.Show(this, "Wrong user data",
                                "Warning", MessageBoxButton.OK, MessageBoxImage.Warning);
                pass1_field.Clear();
                pass2_field.Clear();
            }
        }

        private void Cancel_btn_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }
    }
}
